package com.haoting.mamba.news.service.impl;

import com.haoting.mamba.news.entity.NewsSubjectEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoting.common.utils.PageUtils;
import com.haoting.common.utils.Query;

import com.haoting.mamba.news.dao.NewsDao;
import com.haoting.mamba.news.entity.NewsEntity;
import com.haoting.mamba.news.service.NewsService;


@Service("newsService")
public class NewsServiceImpl extends ServiceImpl<NewsDao, NewsEntity> implements NewsService {

    @Override
    public void updatesupportsum(String newsId) {
        baseMapper.updatesupportsum(newsId);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        String TimeStart = params.get("query").toString().replace("Z", " UTC");
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
//        Date callbackTimeStart = format.parse(TimeStart);
        QueryWrapper<NewsEntity> wrapper  = new QueryWrapper<>();
        if(!String.valueOf(params.get("query")).equals("")){
            wrapper.like("created",params.get("query"));
        }
        if(!String.valueOf(params.get("key")).isEmpty()){
            wrapper.like("title",params.get("key"));
        }
        IPage<NewsEntity> page = this.page(
                new Query<NewsEntity>().getPage(params),
                wrapper.orderByDesc("created")
        );


        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageall(Map<String, Object> params) {
        IPage<NewsEntity> page = this.page(
                new Query<NewsEntity>().getPage(params),
                new QueryWrapper<NewsEntity>()
                .orderByDesc("created")
        );
        List<NewsEntity> records = page.getRecords();
        for (int i = 0; i < records.size(); i++) {
            if(records.get(i).getType()==2){
                NewsEntity temp = records.get(i);
                records.remove(i);
                records.add(0,temp);

                break;
            }
        }
        return new PageUtils(page);
    }
}